﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.IO.Ports;
using TCA_User.Common;
using TCA_User.Tab_Class;

namespace TCA_User
{
    public partial class Bill_List
    {
        private System.Windows.Forms.TabPage Card_Borrow_Tab;
        private Gridview_Grp Card_Borrow_Tbl;
        private ComboBox_Lbl Card_List;
        private DataTable Card_Tbl;

        private ComboBox_Lbl User_List;
        private DataTable User_Tbl;

        private DatePick_LBL From_Date;
        private DatePick_LBL To_Date;

        private Button Borrow_Add_BT;

        public bool Init_Card_Borrow_Tab()
        {
            PosSize possize = new PosSize(); ;
            string tab_name = "Card_Borrow";
            Card_Borrow_Tab = new System.Windows.Forms.TabPage();
            Card_Borrow_Tab.Text = tab_name;
            Card_Borrow_Tab.SuspendLayout();
            Card_Borrow_Tab.Location = new System.Drawing.Point(4, 22);
            Card_Borrow_Tab.Size = new System.Drawing.Size(900, 390);
            Card_Borrow_Tab.Padding = new System.Windows.Forms.Padding(3);
            Card_Borrow_Tab.TabIndex = 4;
            Card_Borrow_Tab.UseVisualStyleBackColor = true;
            Card_Borrow_Tab.ResumeLayout(true);
            Card_Borrow_Tab.PerformLayout();
            this.MainTabControl.Controls.Add(this.Card_Borrow_Tab);

            // Init Taxi_TBL
            possize.pos_x = 10;
            possize.pos_y = 6;
            possize.width = Card_Borrow_Tab.Size.Width - 400;
            possize.height = Card_Borrow_Tab.Size.Height - 10;
            Card_Borrow_Tbl = new Gridview_Grp(Card_Borrow_Tab, "Taxi", possize, AUTO_RESIZE,
                                                TCA_Database_Conn, @"SELECT * FROM dbo.[Card_Borrow_TBL]", AnchorType.RIGHT);

            possize.pos_x = Card_Borrow_Tab.Size.Width - 380;
            possize.pos_y = 6;
            User_Tbl = Account_TBL_Form.Data_dtb;
            User_List = new ComboBox_Lbl(Card_Borrow_Tab, "Card Owner", possize,
                            User_Tbl, "UserName", "UserName", AnchorType.RIGHT);

            possize.pos_x = Card_Borrow_Tab.Size.Width - 380;
            possize.pos_y = 36;
            Card_Tbl = Card_Table_Form.Data_dtb;
            Card_List = new ComboBox_Lbl(Card_Borrow_Tab, "Card List", possize,
                            Card_Tbl, "Card_Provider_Name", "Card_Provider_Name", AnchorType.RIGHT);

            possize.pos_x = Card_Borrow_Tab.Size.Width - 380;
            possize.pos_y = 66;
            From_Date = new DatePick_LBL(Card_Borrow_Tab, "From Date", possize, AnchorType.RIGHT);

            possize.pos_x = Card_Borrow_Tab.Size.Width - 380;
            possize.pos_y = 96;
            To_Date = new DatePick_LBL(Card_Borrow_Tab, "To Date", possize, AnchorType.RIGHT);

            Borrow_Add_BT = new Button();
            Card_Borrow_Tab.Controls.Add(Borrow_Add_BT);
            Borrow_Add_BT.Location = new System.Drawing.Point(Card_Borrow_Tab.Size.Width - 380, 123);
            Borrow_Add_BT.Text = "Add";
            Borrow_Add_BT.Anchor = ((System.Windows.Forms.AnchorStyles)((
                                System.Windows.Forms.AnchorStyles.Top)
                                | System.Windows.Forms.AnchorStyles.Right));
            Borrow_Add_BT.Click += new System.EventHandler(Borrow_AddBT_BT_Click_event);

            return true;
        }

        private void Borrow_AddBT_BT_Click_event(object sender, EventArgs e)
        {
            string value = "";
            string sql_cmd = @"INSERT INTO [TCA_DATABASE].[dbo].[Card_Borrow_TBL] 
                                    ([ID]
                                    ,[UserName]
                                    ,[Card_Provider_Name]
                                    ,[FromDate]
                                    ,[ToDate])
                                    VALUES ";
            value += "'" + DateTime.Now.ToString() + "', ";
            value += "'" + User_List.My_Combo.Text.Trim() + "', ";
            value += "'" + Card_List.My_Combo.Text.Trim() + "', ";
            value += "'" + From_Date.My_picker.Value.ToString("MM/dd/yy") + "', ";
            value += "'" + To_Date.My_picker.Value.ToString("MM/dd/yy") + "'";
            sql_cmd = sql_cmd + "( " + value + ")";

            if (Update_Data_Info(TCA_Database_Conn, sql_cmd) == false)
            {
                MessageBox.Show("Insert Fail", "Error");
            }
            else
            {
                Card_Borrow_Tbl.Refresh_Form();
            }
        }
    }
}